﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Main_AjaUtils_AjaxUploader : System.Web.UI.Page
{

    string oprSessionId = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        oprSessionId = txtTaskSession.Text;
        LoadUploadOperations();
        txtFileUploader.Attributes.Add("onchange", "ReturnFileNameToDiv();");
    }

    private void LoadUploadOperations()
    {
        for (int i = 0; i < Config.UploadOperation.Length; i++)
        {
            ddUploadOperation.Items.Add(Config.UploadOperation[i]);
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        bool uploadSuccess = false;

        if (txtFileUploader.HasFile)
        {
            try
            {
                string fileIndex = FileFunctions.GenerateFileSaveIndex(txtFileUploader.FileName,true);

                string saveDirectory = string.Empty;

                switch(ddUploadOperation.Items[ddUploadOperation.SelectedIndex].Value)
                {
                    case "Task":
                        saveDirectory = Config.GetUserFileDirectory(Config.enumUserFiles.Temp, "user1");

                        if (Session[oprSessionId] == null)
                        {
                            CreateTaskUploadSession();
                        }

                        AddFilePathToTaskUploadSession(saveDirectory + fileIndex);

                        break;
                    case "Mail":
                        break;
                    case "File":
                        break;
                }

                txtFileUploader.SaveAs(saveDirectory + fileIndex);

                uploadSuccess = true;
            }
            catch (Exception ex)
            {
                uploadSuccess = false;
            }
        }

        if (uploadSuccess == true)
        {
            this.RegisterStartupScript("uploadSuccess","<script type=\"text/javascript\">UploadSuccess();</script>");
        }
        else
        {
            //alert upload error
        }
        
    }

    public void CreateTaskUploadSession()
    {
        ArrayList taskUploadSession = new ArrayList();
        Session.Add(oprSessionId, taskUploadSession);
    }

    public void AddFilePathToTaskUploadSession(string filePath)
    {
        ArrayList taskUploadSession = (ArrayList)Session[oprSessionId];
        taskUploadSession.Add(filePath);
    }
}
